package cn.finder0519.simplemediaplayer;

import android.content.Context;
import android.widget.Toast;

import androidx.media3.common.C;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy;

@UnstableApi
public class CustomLoadErrorHandlingPolicy extends DefaultLoadErrorHandlingPolicy {

    private static final int MAX_RETRY_COUNT = 3;
    private final Context context;

    public CustomLoadErrorHandlingPolicy(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    public long getRetryDelayMsFor(LoadErrorInfo loadErrorInfo) {
        if (loadErrorInfo.errorCount <= MAX_RETRY_COUNT) {
            Toast.makeText(context, context.getString(R.string.retrying), Toast.LENGTH_SHORT).show();
            return loadErrorInfo.errorCount * 1000L;
        }
        return C.TIME_UNSET;
    }

    @Override
    public int getMinimumLoadableRetryCount(int dataType) {
        return MAX_RETRY_COUNT;
    }
}